package com.tianbao54.grpc.impl;

import com.tianbao54.grpc.*;
import io.grpc.stub.StreamObserver;

import java.util.UUID;

public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

    @Override
    public void getRealNameByUsername(MyRequest request, StreamObserver<MyResponse> responseObserver) {

        System.out.println("receive message from client : " + request.getUsername());

        responseObserver.onNext(MyResponse.newBuilder().setRealname("zhangsan").build());
        responseObserver.onCompleted();
    }

    @Override
    public void getStudentByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {

        System.out.println("request message from client : " + request.getAge());

        responseObserver.onNext(StudentResponse.newBuilder().setAge(12).setCity("beijing").setName("zhangsan").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(13).setCity("beijing").setName("lisi").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(14).setCity("beijing").setName("wangwu").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(15).setCity("beijing").setName("maliu").build());

        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<StudentRequest> getStudentsWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {

        return new StreamObserver<StudentRequest>() {
            @Override
            public void onNext(StudentRequest value) {
                System.out.println("onNext : " + value.getAge());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                StudentResponse studentResponse1 = StudentResponse.newBuilder().setName("zhangsan").setCity("nanchang").setAge(20).build();
                StudentResponse studentResponse2 = StudentResponse.newBuilder().setName("lisi").setCity("beijing").setAge(20).build();
                StudentResponseList responseList = StudentResponseList.newBuilder()
                        .addStudentResponse(studentResponse1).addStudentResponse(studentResponse2).build();
                responseObserver.onNext(responseList);
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {

        return new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println("value = " + value.getRequestInfo());
                responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {

                responseObserver.onCompleted();
            }
        };
    }
}
